package com.yiba.logutils;

import android.content.Context;
import android.text.TextUtils;

import com.yiba.logutils.help.ExceptionHandler;
import com.yiba.logutils.help.FileHelper;
import com.yiba.logutils.save.impl.LogRecorder;
import com.yiba.logutils.utils.DateUtils;
import com.yiba.logutils.utils.FileUtil;
import com.yiba.logutils.utils.Logs;

import java.io.File;
import java.util.List;

/**
 * 日志管理崩溃框架入口
 * Created by Administrator on 2017/11/21 0021.
 */

public class LogUtils {
    private static LogUtils mLogUtil;
    /**
     * 设置缓存文件夹的大小,默认是50MB
     */
    private long mCacheSize = 50 * 1024 * 1024;

    /**
     * 设置日志保存的路径
     */
    private String mROOT;

    /**
     * 设置日志的保存方式
     */
    private LogRecorder mLogSaver;


    private LogUtils() {
    }


    public static LogUtils getInstance() {
        if (mLogUtil == null) {
            synchronized (LogUtils.class) {
                if (mLogUtil == null) {
                    mLogUtil = new LogUtils();
                }
            }
        }
        return mLogUtil;
    }

    public LogUtils setCacheSize(long cacheSize) {
        this.mCacheSize = cacheSize;
        return this;
    }


    public LogUtils setLogDir(Context context, String logDir) {
        if (TextUtils.isEmpty(logDir)) {
            mROOT = FileUtil.getTempDirPath(context);
        } else {
            mROOT = logDir;
        }
        return this;
    }

    public LogUtils setLogSaver(LogRecorder iRecord) {
        this.mLogSaver = iRecord;
        return this;
    }

    public LogUtils setLogDebugModel(boolean isDebug){
        Logs.isDebug = isDebug;
        return this;
    }


    public String getROOT() {
        return mROOT;
    }

    public void init(Context context) {
        if (TextUtils.isEmpty(mROOT)) {
            mROOT = FileUtil.getTempDirPath(context);
        }
        // init exception collector
        ExceptionHandler.init();
        // check log file size if size>5M 删除 防止上传失败
        try {
            long length=0;
            File file = FileHelper.findFile(mROOT, DateUtils.getDate());
            if(file!=null&&file.exists()){
                length = file.length();
                if (length / 1024 / 1024 > 5) {
                    FileHelper.findFile(mROOT, DateUtils.getDate()).delete();
                }
            }

        }catch (Exception e){
            log(" check log file size if size>5M 删除 防止上传失败", "check log file size if size>5M 删除 防止上传失败"+e.getMessage());
        }
    }


    public long getCacheSize() {
        return mCacheSize;
    }


    /**
     * 检查文件夹是否超出缓存大小，超出则会删除该目录下的所有文件
     *
     * @param dir 需要检查大小的文件夹
     * @return 返回是否超过大小，true为是，false为否
     */

    public synchronized boolean checkCacheSizeAndDelOldestFile(File dir) {
        long dirSize;
        try {
             dirSize = FileUtil.folderSize(dir);
        }catch (Exception e){
            return true;
        }
        return dirSize >= LogUtils.getInstance().getCacheSize() && FileUtil.deleteOldestFile(new File(mLogSaver.dirPath));
    }


    //--------------------------------------增加log函数,供外界调用----------------------------------------------
    public void log(String tag, String msg) {
        log(tag, msg, false);
    }

    public void log(String tag, List<String> msgList) {
        log(tag, msgList, false);
    }

    public void log(String tag, String msg, boolean cryptFlag) {
        if (mLogSaver != null && !TextUtils.isEmpty(tag) && !TextUtils.isEmpty(msg)) {
            mLogSaver.log(tag, msg, cryptFlag);
        }
    }

    public void log(String tag, List<String> msgList, boolean cryptFlag) {
        if (mLogSaver != null && !TextUtils.isEmpty(tag) && msgList != null && msgList.size() > 0) {
            mLogSaver.log(tag, msgList, cryptFlag);
        }
    }



}
